
package cz.muni.fi.PA165.musiclibrarybackend.dao;

import cz.muni.fi.PA165.musiclibrarybackend.entities.Author;
import cz.muni.fi.PA165.musiclibrarybackend.entities.IllegalEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Repository;

/**
 *
 * @author xmacejov
 */
@Repository
public class AuthorDAOImpl implements AuthorDAO, Serializable {
    
    @PersistenceContext
    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }
    
    public void createAuthor(Author author) {
        if (author==null){
            throw new IllegalArgumentException("author is null");
        }
        if(author.getId() != null){
            throw new IllegalEntityException();
        }
        if (author.getName() == null ) {
            throw new IllegalArgumentException("author wihtout name");
        }
        entityManager.persist(author);

    }

    public Author getAuthor(Long id) {
        if(id == null){
              throw new IllegalArgumentException("id is null");
        }
        Author author = entityManager.find(Author.class, id);
        return author;
    }

    public List<Author> getAllAuthors() {
        Query query = entityManager.createQuery(
                "SELECT author FROM Author author");
        List<Author> authors = query.getResultList();
        return authors;
    }

    public void updateAuthor(Author author) {
        
        if(author == null || author.getId() == null || author.getName() == null){
              throw new IllegalArgumentException("author is null");
        }
        
        Author authorToUpdate = entityManager.find(Author.class, author.getId());
        
        if (authorToUpdate == null) {
            throw new IllegalEntityException();
        }
        authorToUpdate.setName(author.getName());
        entityManager.persist(authorToUpdate);

    }

    public void deleteAuthor(Author author) {
        if (author==null){
              throw new IllegalArgumentException("author is null");            
        }
        if (author.getId() == null){
              throw new IllegalEntityException("author id is null");            
        }
        Author authorToDelete = entityManager.find(Author.class, author.getId());

        if (authorToDelete == null) {
            throw new IllegalArgumentException();
        }        
        
        if (!(author.getId().equals(authorToDelete.getId())) ||
                !(authorToDelete.getName().equals(author.getName()))) {
            throw new IllegalArgumentException("trying to delete wrong author");
        }
        

        entityManager.remove(authorToDelete);

        
    }
    
}
